package com.iap.admin.mediator
{
	import com.iap.admin.event.TagEvent;
	import com.iap.common.signal.TagRequestSignal;
	import com.iap.admin.view.TagView;
	import com.iap.admin.view.tag.TagEditView;
	import com.iap.admin.view.tag.code.TagEditViewCode;
	import com.iap.app.AppGlobal;
	import com.iap.app.StatusCode;
	import com.iap.common.model.Tag;
	
	import spark.components.Group;
	import com.iap.common.mediator.BaseMediator;

	public class TagEditMediator extends BaseMediator
	{
		[Inject]
		public var view:TagEditView;
		[Inject]
		public var request:TagRequestSignal;
		
		override public function onRegister():void
		{
			view.onCancel.add(onCancel);
			view.onSubmit.add(onSubmit);
			addContextListener(TagEvent.RESPONSE, responseHandler);
		}
		
		private function onCancel(itemName:String):void
		{
			switch(itemName)
			{
				case TagEditViewCode.CANCEL_NAME:
				{
					optionBack();
					break;
				}
			}
		}
		
		private function optionBack():void
		{
			setView(view.parent as Group, new TagView(), FX_GLIDE_OUT);
		}
		
		private function onSubmit(model:Tag, task:String):void
		{
			AppGlobal.startWait();
			request.dispatch(model, task);
		}
		
		private function responseHandler(evt:TagEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.status.code == StatusCode.OK)
				optionBack();
		}
	}
}